#######################################################################
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

######################################################################
# Easy-setup Enrollee SDK build script
######################################################################
Import('env')

enrollee_env = env.Clone()

target_os = enrollee_env.get('TARGET_OS')

if enrollee_env.get('RELEASE'):
    enrollee_env.AppendUnique(CCFLAGS=['-Os'])
else:
    enrollee_env.AppendUnique(CCFLAGS=['-g'])

######################################################################
# Build flags
######################################################################
enrollee_env.PrependUnique(CPPPATH=[
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/include',
    '#/resource/csdk/logger/include',
    '#/resource/oc_logger/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/logger/include',
    '#/resource/csdk/security/include',
    '#/resource/csdk/connectivity/api',
    '#/extlibs/cjson',
    '#/service/easy-setup/inc',
    '#/service/easy-setup/enrollee/inc',
    '#/service/easy-setup/enrollee/src'
])

if target_os not in ['linux', 'tizen']:
    enrollee_env.PrependUnique(CPPPATH=['#extlibs/sqlite3'])

if enrollee_env.get('SECURED') == '1':
    enrollee_env.AppendUnique(CPPPATH=[
        '#/resource/csdk/security/provisioning/include',
        '#/resource/csdk/security/provisioning/include/internal',
        '#/resource/csdk/security/provisioning/include/oxm'
    ])
    enrollee_env.AppendUnique(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

if target_os not in ['windows']:
    enrollee_env.AppendUnique(CXXFLAGS=['-Wall', '-std=c++0x'])

######################################################################
# Linux Enrollee
######################################################################
if target_os in ['linux']:
    enrollee_env.AddPthreadIfNeeded()
    enrollee_env.AppendUnique(LIBS=['dl'])
    enrollee_env.PrependUnique(LIBS=[
        'oc',
        'octbstack',
        'oc_logger',
        'pthread',
        'connectivity_abstraction',
    ])
    if enrollee_env.get('SECURED') == '1':
        enrollee_env.PrependUnique(LIBS=['ocprovision', 'ocpmapi'])

######################################################################
# Tizen Enrollee
######################################################################
if target_os == 'tizen':
    enrollee_env.AppendUnique(CXXFLAGS=['-pthread'])
    enrollee_env.PrependUnique(LIBS=[
        'oc',
        'octbstack',
        'oc_logger',
        'pthread',
        'connectivity_abstraction',
    ])

######################################################################
# Source files and Targets
######################################################################
es_enrollee_src = None

if target_os in ['linux', 'tizen']:
    es_enrollee_common_src = ['./src/easysetup.c', './src/resourcehandler.c', './src/es_util.c']
    enrollee_sdk_shared = enrollee_env.SharedLibrary('ESEnrolleeSDK',
                                                     es_enrollee_common_src)
    enrollee_sdk_static = enrollee_env.StaticLibrary('ESEnrolleeSDK',
                                                     es_enrollee_common_src)
    enrollee_env.InstallTarget([enrollee_sdk_shared, enrollee_sdk_static],
                               'libESEnrollee')
    enrollee_env.UserInstallTargetLib(
        [enrollee_sdk_shared, enrollee_sdk_static], 'libESEnrollee')
    enrollee_env.UserInstallTargetHeader(
        'inc/ESEnrolleeCommon.h', 'service/easy-setup', 'ESEnrolleeCommon.h')
    enrollee_env.UserInstallTargetHeader('inc/easysetup.h',
                                         'service/easy-setup', 'easysetup.h')

if enrollee_env.get('WITH_TEST') and target_os in ('linux'):
    SConscript('../sampleapp/enrollee/linux/SConscript')
    #Build UnitTestcases for Enrollee
    if enrollee_env.get('SECURED') == '0':
        SConscript('../enrollee/unittests/SConscript')
